package com.doit.day01;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class DatanodeDemo {

    //写一个注册的方法
    public void register(String host) throws Exception {
        ZooKeeper zooKeeper = new ZooKeeper("linux01:2181", 2000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println(host+"的datanode上线了.....开始正常工作");
            }
        });

        //执行注册这个动作，也就是往zk里面创建一个节点
        zooKeeper.create("/datanodes/"+host,host.getBytes(StandardCharsets.UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        //加不加都行
//        Thread.sleep(Long.MAX_VALUE);
    }

    public static void main(String[] args) throws Exception {
        System.out.println("名称叫："+args[0]);
        //执行注册的方法
        new DatanodeDemo().register(args[0]);
    }
}
